// FrogRenameFindReplaceDialog.cpp : Implementation of CFrogRenameFindReplaceDialog

#include "stdafx.h"
#include "FrogRenameFindReplaceDialog.h"
#include "FrogMessageBox.h"

// CFrogRenameFindReplaceDialog
CFrogRenameFindReplaceDialog::CFrogRenameFindReplaceDialog(HICON hIcon):
CFrogProgressReportDialog(hIcon),
m_Find(L""),
m_Replace(L"")
{
}

LRESULT CFrogRenameFindReplaceDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogBaseDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);
	DoDataExchange(FALSE);
	return 0;
}

LRESULT CFrogRenameFindReplaceDialog::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	// TODO: Add your control notification handler code here
	DoDataExchange(TRUE);

	if (m_Find == L"")
	{
		FrogMessageBox(m_hWnd, L"Search text is empty, enter text in order to continue.", L"FilerFrog - Find Replace Error", MB_ICONWARNING | MB_OK);
		::SetFocus(GetDlgItem(IDC_EDIT_FIND));
		return TRUE;
	}

	if (!FilerFrogUtils::IsFileNameValid(wstring(m_Find)))
	{
		FrogMessageBox(m_hWnd, L"A file name cannot contain any of the following characters:\n\\ / : * ? \" < > |", L"File Name Validation", 0);
		return TRUE;
	}

	if (m_Replace != L"" && !FilerFrogUtils::IsFileNameValid(wstring(m_Replace)))
	{
		FrogMessageBox(m_hWnd, L"A file name cannot contain any of the following characters:\n\\ / : * ? \" < > |", L"File Name Validation", 0);
		return TRUE;
	}

	RunAsyncCommandWithProgressBar(false);
	EndDialog(wID);
	return 0;
}

void CFrogRenameFindReplaceDialog::ExecuteCommand()
{
	m_FilesRefactor.RenameFilesFindReplace(m_LastResult, m_FilesList, (wstring)m_Find ,(wstring)m_Replace);
}